package com.hmall.order.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.order.mapper.OrderDetailMapper;
import com.hmall.order.mapper.OrderLogisticsMapper;
import com.hmall.order.mapper.OrderMapper;
import com.hmall.order.pojo.Order;
import com.hmall.order.pojo.OrderDetail;
import com.hmall.order.pojo.OrderLogistics;
import com.hmall.order.pojo.ReqParams;
import com.hmall.order.service.IOrderService;
import com.luozy.common.clients.ItemClient;
import com.luozy.common.clients.UserClient;
import com.luozy.common.context.TrdLocal;
import com.luozy.common.dto.Address;
import com.luozy.common.dto.Item;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;

@Service
public class OrderService extends ServiceImpl<OrderMapper, Order> implements IOrderService {
    @Autowired
    private ItemClient itemClient;
    @Autowired
    private OrderDetailMapper detailMapper;
    @Autowired
    private UserClient userClient;
    @Autowired
    private OrderLogisticsMapper orderLogisticsMapper;

    //提交订单
    @Override
    public Long submitOrder(ReqParams reqParams) {
        //1、根据雪花算法生成订单id（使用MybatisPlus注解）
        //2、商品微服务提供FeignClient，实现根据id查询商品的接口
        //3、根据itemId查询商品信息
        Item item = itemClient.getById(reqParams.getItemId());
        //4、基于商品价格、购买数量计算商品总价：totalFee
        Long totalFee = item.getPrice() * reqParams.getNum();
        //5、封装Order对象，初始status为未支付
        Order order = new Order();
        order.setTotalFee(totalFee);
        order.setPaymentType(reqParams.getPaymentType());
        order.setUserId(TrdLocal.threadLocal.get());
        order.setStatus(1);
        //6、将Order写入数据库tb_order表中
        this.save(order);
        //7、将商品信息、orderId信息封装为OrderDetail对象，写入tb_order_detail表
        OrderDetail orderDetail = new OrderDetail();
        orderDetail.setOrderId(order.getId());
        orderDetail.setItemId(reqParams.getItemId());
        orderDetail.setNum(reqParams.getNum());
        orderDetail.setName(item.getName());
        orderDetail.setPrice(item.getPrice());
        orderDetail.setSpec(item.getSpec());
        orderDetail.setImage(item.getImage());
        detailMapper.insert(orderDetail);
        //8、将user-service的根据id查询地址接口封装为FeignClient
        //9、根据addressId查询user-service服务，获取地址信息
        Address address = userClient.findAddressById(reqParams.getAddressId());
        //10、将地址封装为OrderLogistics对象，写入tb_order_logistics表
        OrderLogistics orderLogistics = new OrderLogistics();
        BeanUtils.copyProperties(address, orderLogistics);
        orderLogistics.setOrderId(order.getId());
        //orderLogistics.setLogisticsNumber(LocalDateTime.now().toString());
        orderLogistics.setLogisticsNumber(new Date().toString());
        orderLogistics.setLogisticsCompany("顺丰");
        orderLogisticsMapper.insert(orderLogistics);
        //11、在item-service提供减库存接口，并编写FeignClient
        //12、调用item-service的减库存接口
        itemClient.deductStock(reqParams.getItemId(), reqParams.getNum());
        return order.getId();
    }
}
